<?php
/*
 * <author>Jiří Němec</author>
 * <date>2009-01-16</date>
 * <modified>2009-07-10</modified>
 */

/* formát: obrázek.připona-w1024h768q95e0c1.jpg
 * w - šířka
 * h - výška
 * q - kvalita jpeg
 * e - zanechání exif (0 - oříznout exif)
 * c - ořez na přesně zadanou velikost
 */



if (eregi('^(.+)/([^/]+)\.(gif|jpg|png|jpeg)-([0-9whqec]+)\.(gif|jpg|png|jpeg)$',urldecode(ereg_replace('^'.ereg_replace('[a-z]+.php$','',$_SERVER["SCRIPT_NAME"]),'',$_SERVER["REQUEST_URI"])),$out)) {

 $outputName = $out[0];

 $dir = $out[1];
 $srcFileName = $out[2].'.'.$out[3];
 $srcFileNameExtension = strtolower($out[3]);
 $dstExtension = $out[5];

 if (!file_exists($dir.DIRECTORY_SEPARATOR. $srcFileName)) {
  echo 'Chyba pří čtení zdrojového souboru!';
  die();
 }

 $key = '';
 $paramA = array();
 for($i = 0;$i < strlen($out[4]);$i++) {
  if (ereg('[a-z]',$out[4][$i])) $key = $out[4][$i];
  else $paramA[$key] .= $out[4][$i];
 }
 foreach ($paramA as &$value) {
  $value = (int) $value;
 }

 $width = $height = $newWidth = $newHeight = 0;
 list($width,$height)=getimagesize($dir.DIRECTORY_SEPARATOR. $srcFileName);
 if($width <=0 ||  $height <= 0) {
  echo 'Chyba pří čtení velikosti zdrojového souboru!';
  die();
 }
  // vypocet rozmeru
 if (isset($paramA['h']) && isset($paramA['w'])) {
  $newHeight=$paramA['h'];
  $newWidth=$paramA['w'];
 }elseif (isset($paramA['h'])) {
  $newHeight=$paramA['h'];
  $newWidth=ceil(($width/$height)*$paramA['h']);
 }elseif (isset($paramA['w'])) {
  $newWidth=$paramA['w'];
  $newHeight=ceil(($height/$width)*$paramA['w']);
 } else {
  $newWidth = $width;
  $newHeight = $height;
 }
 if (($newWidth > $width) && ($newHeight > $height)) {
  $newWidth = $width;
  $newHeight = $height;
 }

 if($newWidth <=0 ||  $newHeight <= 0) {
  echo 'Chyba pří výpočtu nových rozměrů!';
  die();
 }

 // zpracovani imagemagickem
 if(isset($paramA['c']) && $paramA['c'] == 1) {
  $cmd = ('convert '.(isset($paramA['e']) && $paramA['e'] == 0?'-strip ':'').'-quality '.(isset($paramA['q'])?$paramA['q']:92).' -filter Welsh -thumbnail x'.($newHeight*2).' -resize \''.($newWidth*2).'x<\' -resize 50% -gravity center -crop '.$newWidth.'x'.$newHeight.'+0+0 +repage '.$dir.DIRECTORY_SEPARATOR. $srcFileName.' '.$outputName);
 }
 else {
  $cmd = ('convert '.(isset($paramA['e']) && $paramA['e'] == 0?'-strip ':'').'-quality '.(isset($paramA['q'])?$paramA['q']:92).' -filter Welsh -resize '.$newWidth.'x'.$newHeight.' '.$dir.DIRECTORY_SEPARATOR. $srcFileName.' '.$outputName);
 }
 $ret = 1;
 system($cmd,$ret);
 // když to nejde, tak knihovnou GD
 if ($ret != 0) {
  $cropLeft = 0;
  $cropHeight = 0;
  if(isset($paramA['c']) && $paramA['c'] == 1) {
   if(($width/$newWidth) > ($height/$newHeight)) {
    $cropLeft = ($width - ($height/$newHeight)*$newWidth)/2;
    $width -= ceil(2*$cropLeft);
    $cropLeft = ceil($cropLeft);
   }else {
    $cropHeight = ($height - ($width/$newWidth)*$newHeight)/2;
    $height -= ceil(2*$cropHeight);
    $cropHeight = ceil($cropHeight);

   }
  } else {
   $fwidth = $newWidth;
   $fheight = ceil(($newWidth/$width)*$height);
   if ($fheight > $newHeight) {
    $fheight = $newHeight;
    $fwidth = ceil(($newHeight/$height)*$width);
   }
   $newWidth = $fwidth;
   $newHeight = $fheight;
  }

  if ($srcFileNameExtension=='jpeg' || $srcFileNameExtension=='jpg') {
   $src = imagecreatefromjpeg($dir.DIRECTORY_SEPARATOR.$srcFileName);
  } elseif ($srcFileNameExtension=='gif') {
   $src = imagecreatefromgif($dir.DIRECTORY_SEPARATOR. $srcFileName);
  } elseif ($srcFileNameExtension=='png') {
   $src = imagecreatefrompng($dir.DIRECTORY_SEPARATOR. $srcFileName);
  }
  $tmp=imagecreatetruecolor($newWidth,$newHeight);
  imagecopyresampled($tmp,$src,0,0,$cropLeft,$cropHeight,$newWidth,$newHeight,$width,$height);
  if (strtolower($dstExtension)=='jpeg' || strtolower($dstExtension)=='jpg') {
   imagejpeg($tmp,$outputName,(isset($paramA['q'])?$paramA['q']:94));
  } else if (strtolower($dstExtension)=='gif') {
    imagegif($tmp,$outputName);
   } elseif (strtolower($dstExtension)=='png') {
    imagepng($tmp,$outputName);
   }
 }
 // vypis obrazku
 if (file_exists($outputName) ) {
  chmod($outputName,0666);
  if (strtolower($dstExtension)=='jpeg' || strtolower($dstExtension)=='jpg') {
   header("Content-type:image/jpeg");
  } else if (strtolower($dstExtension)=='gif') {
    header("Content-type:image/gif");
   } elseif (strtolower($dstExtension)=='png') {
    header("Content-type:image/png");
   }
  readfile($outputName);
 }
}

?>
